-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
60 lines (52 loc) · 1.53 KB
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#define INF 99999
#define MAX 100
void floydWarshall(int graph[MAX][MAX], int vertices) {
int dist[MAX][MAX];
// Initialize distance array
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
dist[i][j] = graph[i][j];
}
}
// Compute shortest paths
for (int k = 0; k < vertices; k++) {
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
// Print the result
printf("Shortest distances between every pair of vertices:\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
if (dist[i][j] == INF)
printf("INF ");
else
printf("%3d ", dist[i][j]);
}
printf("\n");
}
}
int main() {
int vertices, edges;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &vertices, &edges);
int graph[MAX][MAX];
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
graph[i][j] = (i == j) ? 0 : INF;
}
}
printf("Enter the edges (u v w):\n");
for (int i = 0; i < edges; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
graph[u][v] = w;
}
floydWarshall(graph, vertices);
return 0;
}